<?php
/**
 * CustomerUsersController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 06:30:06
 * @modified    2023-02-14 06:30:06
 *
 */

namespace App\Http\Controllers;

use App\Repositories\Customer\UserRepo;
use Illuminate\Http\Request;

class CustomerUsersController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/customer_users",
     *     tags={"CustomerUser"},
     *     summary="获取客户联系人列表",
     *     description="获取客户联系人列表",
     *     operationId="customer_user_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="keyword", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="name", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="customer_tag", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="customer_source", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="customer_save_level", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="gender", in="query", description="性别", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="phone", in="query", description="联系电话", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="office_phone", in="query", description="办公电话", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="email", in="query", description="电子邮箱", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="duty", in="query", description="职务", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="customer_id", in="query", description="所属单位ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="province_id", in="query", description="所在省份ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="city_id", in="query", description="所在城市ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="county_id", in="query", description="所在区县ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default=0, type="状态")),
     *     @OA\Parameter(name="created_at", in="query", description="创建时间", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $users = UserRepo::getList();
        return list_message("获取成功", true, $users);
    }

    /**
     * @OA\Post(
     *     path="/customer_users",
     *     tags={"CustomerUser"},
     *     summary="添加客户联系人",
     *     description="添加客户联系人",
     *     operationId="customer_user_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "gender", "phone", "office_phone", "email", "birthday", "duty", "project_role", "customer_id", "country_id", "province_id", "city_id", "county_id", "address", "status", "is_default"},
     *              @OA\Property(property="event_id", type="integer", example="事件ID", description="事件ID"),
     *              @OA\Property(property="name", type="string", example="", description="姓名"),
     *              @OA\Property(property="gender", type="string", example="", description="性别"),
     *              @OA\Property(property="phone", type="string", example="", description="联系电话"),
     *              @OA\Property(property="office_phone", type="integer", example="", description="办公电话"),
     *              @OA\Property(property="email", type="integer", example="", description="电子邮箱"),
     *              @OA\Property(property="birthday", type="integer", example="", description="出生日期"),
     *              @OA\Property(property="duty", type="string", example="", description="职务"),
     *              @OA\Property(property="project_role", type="string", example="", description="项目角色"),
     *              @OA\Property(property="customer_id", type="string", example="", description="客户单位ID"),
     *              @OA\Property(property="province_id", type="integer", example="", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="", description="详细地址"),
     *              @OA\Property(property="status", type="integer", example="", description="状态(0|1)"),
     *              @OA\Property(property="is_default", type="integer", example="", description="是否默认联系人(0|1), 客户没有联系人时，无论是否选是，都设置为默认联系人，有联系人时则根据该选项修改默认联系人"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $user = UserRepo::store(0);
            return message("创建成功", true, $user);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/customer_users/{id}",
     *     tags={"CustomerUser"},
     *     summary="获取客户联系人",
     *     description="获取客户联系人",
     *     operationId="customer_user_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户联系人ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $customer = UserRepo::one($id);
            return message("获取成功", true, $customer);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/customer_users/info_phone/{phone}",
     *     tags={"CustomerUser"},
     *     summary="根据电话号码获取客户信息",
     *     description="根据电话号码获取客户信息",
     *     operationId="customer_user_info_phone",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="phone",in="path",description="客户联系人电话",required=true,@OA\Schema(default="",type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function info_phone(Request $request, $telephone)
    {
        try {
            $info = UserRepo::infoByPhone($telephone);
            return message('获取成功', 1, $info);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, []);
        }
    }

    /**
     * @OA\Put(
     *     path="/customer_users/{id}",
     *     tags={"CustomerUser"},
     *     summary="更新客户联系人",
     *     description="更新客户联系人",
     *     operationId="customer_user_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户联系人ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "gender", "phone", "office_phone", "email", "birthday", "duty", "project_role", "customer_id", "country_id", "province_id", "city_id", "county_id", "address", "status", "is_default"},
     *              @OA\Property(property="name", type="string", example="", description="姓名"),
     *              @OA\Property(property="gender", type="string", example="", description="性别"),
     *              @OA\Property(property="phone", type="string", example="", description="联系电话"),
     *              @OA\Property(property="office_phone", type="integer", example="", description="办公电话"),
     *              @OA\Property(property="email", type="integer", example="", description="电子邮箱"),
     *              @OA\Property(property="birthday", type="integer", example="", description="出生日期"),
     *              @OA\Property(property="duty", type="string", example="", description="职务"),
     *              @OA\Property(property="project_role", type="string", example="", description="项目角色"),
     *              @OA\Property(property="customer_id", type="string", example="", description="客户单位ID"),
     *              @OA\Property(property="province_id", type="integer", example="", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="", description="详细地址"),
     *              @OA\Property(property="status", type="integer", example="", description="状态(0|1)"),
     *              @OA\Property(property="is_default", type="integer", example="", description="是否默认联系人(0|1), 客户没有联系人时，无论是否选是，都设置为默认联系人，有联系人时则根据该选项修改默认联系人"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $user = UserRepo::store($id);
            return message("更新成功", true, $user);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/customer_users/{id}",
     *     tags={"CustomerUser"},
     *     summary="删除客户联系人",
     *     description="删除客户联系人",
     *     operationId="customer_user_delete",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户联系人ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            UserRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/customer_users/params",
     *     tags={"CustomerUser"},
     *     summary="获取客户联系人参数列表",
     *     description="获取客户联系人参数列表",
     *     operationId="customer_user_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params(Request $request)
    {
        $params = UserRepo::params();
        return message("获取成功", true, $params);
    }
}